package com.gears_software.sxdc.dccx.data.finder.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.devspark.appmsg.AppMsg;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.activity.LoginActivity_;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.androidannotations.rest.spring.api.RestErrorHandler;
import org.springframework.core.NestedRuntimeException;


/**
 * Created by lileltp on 2017/4/8.
 */
@EBean
public class GearsErrorHandler implements RestErrorHandler {

    @RootContext
    Activity context;
    @App
    DCCXApplication app;
    @Pref
    DCCXSp_ sp;


    @Override
    public void onRestClientExceptionThrown(NestedRuntimeException e) {
        ShowErrorInfo(e.getMessage());
    }

    @UiThread
    void ShowErrorInfo(String errorMessage) {
        Logger.d(errorMessage);
        if (errorMessage.contains("failed to connect")) {
            errorMessage = "无法连接服务器";
        }
        if (errorMessage.contains("Unable to resolve host")) {
            errorMessage = "请检查网络状态,无法连接服务器";
        }
        if (errorMessage.contains("403 Forbidden")) {
            errorMessage = "登录失效，请重新登录";
            if (!app.needLogin) {
                return;
            }
        }
        TastyToast.makeText(context, errorMessage, app.SHORT, app.ERROR);
    }
}
